package com.bluedot.action;

import java.util.List;

import com.bluedot.dao.UserDaoImpl;
import com.bluedot.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LogonAction extends ActionSupport {
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String execute() throws Exception {
		UserDaoImpl ud = new UserDaoImpl();

		List<User> list = ud.findUserByName(user.getUserName());
		if (list == null ||list.size() == 0) {
			return "lost";
		} 
		else {
			ActionContext.getContext().getSession().put("user", list.get(0));
			return SUCCESS;
		}
	}

	@Override
	public void validate() {
		if (user == null ||user.getUserName().length() == 0) {
			addFieldError("user.userName", "用户名问题");
		}
		if (user.getPassword() == null || user.getPassword().length() == 0) {
			addFieldError("user.password", "密码错误");
		}

	}
}
